package com.smart.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.time.LocalDate;
import java.util.*;

import com.smart.common.core.domain.VehicleTripInfo;
import com.smart.common.core.page.PageQuery;
import com.smart.common.exception.ServiceException;
import com.smart.common.utils.DateUtils;
import com.smart.common.utils.StringUtils;
import com.smart.system.domain.AreaCar;
import com.smart.system.domain.Car;
import com.smart.system.domain.vo.CarMileageAndOilVo;
import com.smart.system.domain.vo.CarTrajectoryVo;
import com.smart.system.service.IAreaCarService;
import com.smart.system.service.ICarService;
import com.smart.system.service.IMobileUnitService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.smart.system.mapper.CarTrajectoryMapper;
import com.smart.system.domain.CarTrajectory;
import com.smart.system.service.ICarTrajectoryService;

/**
 * 车辆移动轨迹Service业务层处理
 *
 * @author smart
 * @date 2024-11-01
 */
@Service
public class CarTrajectoryServiceImpl extends ServiceImpl<CarTrajectoryMapper, CarTrajectory> implements ICarTrajectoryService {
    @Autowired
    private CarTrajectoryMapper carTrajectoryMapper;

    @Autowired
    private IMobileUnitService mobileUnitService;

    @Autowired
    private ICarService carService;

    @Autowired
    private IAreaCarService areaCarService;

    /**
     * 查询车辆移动轨迹
     *
     * @param id 车辆移动轨迹主键
     * @return 车辆移动轨迹
     */
    @Override
    public CarTrajectory selectCarTrajectoryById(String id) {
        return carTrajectoryMapper.selectById(id);
    }

    /**
     * 查询车辆移动轨迹列表
     *
     * @param carTrajectory 车辆移动轨迹
     * @return 车辆移动轨迹
     */
    @Override
    public List<CarTrajectory> selectCarTrajectoryList(CarTrajectory carTrajectory) {
        return this.list();
    }

    /**
     * 新增车辆移动轨迹
     *
     * @param carTrajectory 车辆移动轨迹
     * @return 结果
     */
    @Override
    public boolean insertCarTrajectory(CarTrajectory carTrajectory) {
            carTrajectory.setCreateTime(DateUtils.getNowDate());
            return this.save(carTrajectory);
    }

    /**
     * 修改车辆移动轨迹
     *
     * @param carTrajectory 车辆移动轨迹
     * @return 结果
     */
    @Override
    public boolean updateCarTrajectory(CarTrajectory carTrajectory) {
                carTrajectory.setUpdateTime(DateUtils.getNowDate());
        return this.updateById(carTrajectory);
    }

    /**
     * 批量删除车辆移动轨迹
     *
     * @param ids 需要删除的车辆移动轨迹主键
     * @return 结果
     */
    @Override
    public boolean deleteCarTrajectoryByIds(String[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }

    /**
     * 删除车辆移动轨迹信息
     *
     * @param id 车辆移动轨迹主键
     * @return 结果
     */
    @Override
    public boolean deleteCarTrajectoryById(String id) {
        return this.removeById(id);
    }

    @Override
    public IPage<CarTrajectoryVo> getCarMovingTrajectory(PageQuery query, Date startTime, Date endTime, Integer carId) {
        IPage<Car> page = carService.page(query.toPage(),Wrappers.lambdaQuery(Car.class)
                .eq(carId!=null,Car::getId,carId));
        List<Car> records = page.getRecords();
        List resList = new LinkedList<>();
        for (Car record : records) {
            CarTrajectoryVo carTrajectoryVo = new CarTrajectoryVo();
            //获取车辆区域Id
            AreaCar one = areaCarService.getOne(Wrappers.lambdaQuery(AreaCar.class).eq(AreaCar::getCarId, record.getId()).last("limit 1"),false);
            if(one != null){
                carTrajectoryVo.setAreaId(one.getAreaId()+"");
            }
            BeanUtils.copyProperties(record,carTrajectoryVo);
            List<CarTrajectory> carTrajectories = this.list(Wrappers.lambdaQuery(CarTrajectory.class)
                    .eq(CarTrajectory::getCarId,carTrajectoryVo.getId())
                    .ge(startTime!=null,CarTrajectory::getCreateTime,startTime)
                    .le(endTime!=null,CarTrajectory::getCreateTime,endTime)
                    .orderByDesc(CarTrajectory::getCreateTime)
                    .last(startTime==null || endTime==null , "limit 12"));
            for (int i = 0; i < carTrajectories.size(); i++) {
                if(i==0){
                    carTrajectoryVo.setCarTrajectory(carTrajectories.get(i));
                }
                carTrajectories.get(i).setSort(i+1);
            }
            carTrajectoryVo.setCarTrajectories(carTrajectories);
            resList.add(carTrajectoryVo);
        }
        IPage resPage = new Page();
        BeanUtils.copyProperties(page,resPage);
        resPage.setRecords(resList);
        return resPage;
    }

    @Override
    public CarTrajectory getNewData(Integer carId) {
        CarTrajectory one = this.getOne(Wrappers.lambdaQuery(CarTrajectory.class)
                .eq(CarTrajectory::getCarId, carId)
                .orderByDesc(CarTrajectory::getCreateTime)
                        .last("limit 1")
                , false);
        return one;
    }

    @Override
    public CarMileageAndOilVo getCarMileageAndOil(String carNumber, String startTime, String endTime) {
        Car one = carService.getOne(Wrappers.lambdaQuery(Car.class).eq(Car::getCarNumber, carNumber).last("limit 1"));
        if(one == null){
            throw new ServiceException("车辆编号不存在!");
        }
        List<VehicleTripInfo> mileageStatement = mobileUnitService.getMileageStatement(Arrays.asList(carNumber), startTime, endTime);
        if (mileageStatement.size()==0) {
            return new CarMileageAndOilVo();
        }
        VehicleTripInfo vehicleTripInfo = mileageStatement.get(0);
        CarMileageAndOilVo carMileageAndOilVo = new CarMileageAndOilVo();
        carMileageAndOilVo.setNumber(vehicleTripInfo.getVehiIdno());

        carMileageAndOilVo.setLicencePlate(one.getLicencePlate());
        carMileageAndOilVo.setOilLevel(vehicleTripInfo.getYouLiang());
        carMileageAndOilVo.setMileage(vehicleTripInfo.getLiCheng());
        return carMileageAndOilVo;
    }

}